using MediatR;
using Microsoft.AspNetCore.Mvc;
using SupplierCustomerManagement.Read.API.Applications.Commands.reportforms;
using SupplierCustomerManagement.Read.API.Dtos;
using B.S.RbacData.ErrorCode;
using SupplierCustomerManagement.Read.API.Dtos.ReportForms;

namespace SupplierCustomerManagement.Read.API.Controllers
{
    /// <summary>
    /// 报表查询控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class ReportFormsController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<ReportFormsController> _logger;

        public ReportFormsController(IMediator mediator, ILogger<ReportFormsController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// 获取存栏统计汇总列表
        /// </summary>
        [HttpGet]
        public async Task<ActionResult<ApiPaging<SheepInventorySummaryListDto>>> GetSheepInventorySummaries([FromQuery] GetSheepInventorySummariesQuery query)
        {
            try
            {
                var result = await _mediator.Send(query);
                _logger.LogInformation("成功获取存栏统计汇总列表，返回 {Count} 条", result.PageData?.Count ?? 0);
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取存栏统计汇总列表失败");
                var error = new ApiPaging<SheepInventorySummaryListDto>
                {
                    code = ResultEnumCode.fails,
                    message = $"获取存栏统计汇总列表失败: {ex.Message}",
                    TotalCount = 0,
                    PageCount = 0,
                    PageData = new List<SheepInventorySummaryListDto>()
                };
                return StatusCode(500, error);
            }
        }

        /// <summary>
        /// 获取种母生产统计-概况
        /// </summary>
        [HttpGet]
        public async Task<ActionResult<ApiResponse<ProductionOverviewDto>>> GetProductionOverview([FromQuery] GetProductionOverviewQuery query)
        {
            try
            {
                var data = await _mediator.Send(query);
                return Ok(ApiResponse<ProductionOverviewDto>.Ok(data));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取种母生产统计-概况失败");
                return StatusCode(500, ApiResponse<ProductionOverviewDto>.Fail($"获取种母生产统计-概况失败: {ex.Message}"));
            }
        }

        /// <summary>
        /// 获取种母生产统计-表格概况
        /// </summary>
        [HttpGet]
        public async Task<ActionResult<ApiResponse<List<ProductionTableRowDto>>>> GetProductionTableOverview([FromQuery] GetProductionTableOverviewQuery query)
        {
            try
            {
                var data = await _mediator.Send(query);
                return Ok(ApiResponse<List<ProductionTableRowDto>>.Ok(data));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取种母生产统计-表格概况失败");
                return StatusCode(500, ApiResponse<List<ProductionTableRowDto>>.Fail($"获取种母生产统计-表格概况失败: {ex.Message}"));
            }
        }
    }
}

